﻿#建议保存编码为：bom头 + utf8
param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("cname")][String]$cluster名
)

& "${PSScriptRoot}/check_cds.ps1" $cluster名
if ($LASTEXITCODE -eq 2)
{
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：已有这个cluster,或cluster名字不合法。退出码1'
	exit 1
}

& "${PSScriptRoot}/_bak_cds.ps1"

mkdir "${PSScriptRoot}/${cluster名}"
& "${PSScriptRoot}/_new_eds.ps1" $cluster名

#合并两个json。cds_1st.json，cds.json
$private:cds_new = Get-Content -LiteralPath "${PSScriptRoot}/cds_1st.json" | ConvertFrom-Json -Depth 99
foreach ($temp001 in $private:cds_new.resources)
{
	$temp001.name = ${cluster名}
	$temp001.'eds_cluster_config'.'service_name' = ${cluster名}
	$temp001.'eds_cluster_config'.'eds_config'.'path' = "/etc/envoy/${cluster名}/eds.json"
}

$private:cds = Get-Content -LiteralPath "${PSScriptRoot}/cds.json" | ConvertFrom-Json -Depth 99
$private:cds.resources += $temp001
$private:cds2 = $private:cds | ConvertTo-Json -Depth 99
Set-Content -LiteralPath "${PSScriptRoot}/cds.json" -Value $private:cds2 -Encoding utf8NoBOM

& "${PSScriptRoot}/_flashfile.ps1" "${PSScriptRoot}/cds.json"
& "${PSScriptRoot}/Write-Host2.ps1"  -ForegroundColor Green '信息：添加cluster成功'
exit 0
